package com.service;

import com.proto.*;
import io.grpc.StatusRuntimeException;
import net.devh.boot.grpc.client.inject.GrpcClient;
import org.springframework.stereotype.Service;

/**
 * @author xiaoshu
 */
@Service
public class GrpcClientService {


    @GrpcClient("local-grpc-server")
    private Service01Grpc.Service01BlockingStub service01Stub;


    public String method01(final String name) {
        try {
            final Method01Reply response = service01Stub.method01(
                    Method01Request.newBuilder().setName(name).build());
            return response.getMessage();
        } catch (final StatusRuntimeException e) {
            return "FAILED with " + e.getStatus().getCode().name();
        }
    }


    public String method02(final String param1,
                           final String param2) {
        try {
            final Method02Reply response = service01Stub.method02(
                    Method02Request.newBuilder()
                            .setParam1(param1)
                            .setParam2(param2)
                            .build());
            return response.getParam1();
        } catch (final StatusRuntimeException e) {
            return "FAILED with " + e.getStatus().getCode().name();
        }
    }


}